<?php
require_once 'common.php';
require_once 'OaDeveloperModel.php';
require_once 'OaAppModel.php';
require_once 'OaCompanyModel.php';
require_once 'OaCodeModel.php';
require_once 'OaAppConfigOrderModel.php';
class OaWhiteIplistModel extends OpModel
{
	
    // 仅用于sandbox环境

	private $_oaWebDb;
    private $_configEnv;
    private $_whiteIplistTable;
    
    public function __construct($configEnv = OaAppModel::APP_CONFIG_ENV_PRODUCTION, $logger = null)
    {
        if (is_null($logger)) {
            $logger = getOaModelLogger();
        }
        parent::__construct($logger);
        $dbConfig = Oa_Util_Config::getSingleDbConfig(OA_WEB_DB_CLUSTER_ID);
        $db = OpDbModel::getInstance($dbConfig, $logger);
        $this->_oaWebDb = $db;
        $this->_configEnv = $configEnv;
        $this->_whiteIplistTable = new OpTableModel($db, 'white_ip_list', $logger);
    }
    
    /**
     * 根据compnay_Id，查询当前加入到白名单的IP
     * @param int $company_Id
     * @return array|null|boolean
     */
    public function getWhiteIplistByCompanyId($where = null, $offset = 0, $count = null, $sort = null, $otherWhere = null)
    {
    	$colum = array('id','company_id','ip_address','create_time');
    	$iplist = $this->_whiteIplistTable->get($where, $offset, $count, $sort, $otherWhere);
    	return $iplist;
    	
    }
    
    /**
     * 查询当前加入到白名单的IP总数
     * @where 查询条件
     * @otherwhere 其他查询条件
     * @return int
     */
    public function getWhiteIplistCount($where, $otherWhere = null)
    {
    	return $this->_whiteIplistTable->getCount($where,$otherWhere);
    }
    /**
     * 查询当前所有的白名单IP
     * @param $where 查询条件
     * @offset limit begin
     * @count 取的数量
     * @sort 排序规则
     * @otherWhere 其他条件
     * @return array|null|boolean
     */
    public function getAllWhiteIplist($where = null, $offset = 0, $count = null, 
        $sort = null, $otherWhere = null)
    {
    	$colum = array('id','company_id','ip_address','create_time');
    	$iplist = $this->_whiteIplistTable->get($where,$offset,$count,$sort,$otherWhere,$colum);
    	
    	return $iplist;
    	
    }

    /**
     * 查询该IP是否在白名单中
     * @param string $ip
     * @return boolean
     */
    public function checkWhiteIplist($ip)
    {
    	$colum = array('ip_address');
    	$iplist = $this->_whiteIplistTable->getRow(array(
    	'ip_address'=>$ip),null,$colum);
    	
    	if (count($iplist) == 1){
    		return true;
    	}else{
    		return false;
    	}	
    }
    
    /**
     *  新增白名单
     * @param string $ip
     * @param int company_id
     * @return boolean
     */
    public function addWhiteIplist($ip,$company_id)
    {
    	$colum = array('ip_address');
    	$iplist = $this->_whiteIplistTable->getRow(array(
    	'ip_address'=>$ip,
    	'company_id'=>$company_id),null,$colum);
    	
    	if (count($iplist['ip_address']) ==1) {
    		$this->_setError(1, "ip company_id duplicated");
    		return false;
    	}
  	
    	$insertId = $this->_whiteIplistTable->insert(array(
    		'ip_address'=>$ip,
    		'company_id'=>$company_id,
    		'create_time'=>date('Y-m-d H:i:s')
    	));
    	if ( $insertId == false) {
    		$this->_logger->log("insert white iplist failed,ip:".$ip.",companyid:".$company_id);
    		return false;
    	} 
    	return true;	
    }
    
    /**
     *  修改白名单
     * @param string $oldIp
     * @param string $newIp
     * @param int company_id
     * @return boolean
     */
    public function modifyWhiteIplist($id,$company,$ip)
    {
        $colum = array('ip_address');
    	$iplist = $this->_whiteIplistTable->getRow(array(
    	'ip_address'=>$ip,
    	'company_id'=>$company),null,$colum);
    	
    	if (count($iplist['ip_address']) ==1) {
    		$this->_setError(1, "ip company_id duplicated");
    		return false;
    	}
    	
    	$colum = array('id');
    	$iplist = $this->_whiteIplistTable->getRow(array(
    	'id'=>$id),null,$colum);
   
    	if (count($iplist) == 0 ){
    		$this->_logger->log("not exist int iplist. id:".$id);
    		return false;
    	}else{
    		return ( $this->_whiteIplistTable->update(array(
    			'id'=>$id),
    			array(
    			'company_id'=>$company,
    			'ip_address'=>$ip,
    			'create_time'=>date('Y-m-d H:i:s')
    		)));
    	}	
    }
    
    /**
     *  删除白名单
     * @param string $ip
     * @param int company_id
     * @return boolean
     */
    public function deleteWhiteIplist($id)
    {
    	return $this->_whiteIplistTable->delete(array(
    	'id'=>$id
    	));	
    }   
}
?>
